import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs';
import {createRouter,createWebHistory} from 'vue-router';
import {userStore} from "../stores/userStore";

const routes =[
    {path:"/",          //添加首页路由，自动跳转到首页
     name:'Index',
     title:'首页',
     component:()=>import("../views/2_index.vue")},
    {path:"/3_register",
     name:'Register',
     title:'注册',
     component:()=>import("../views/3_register.vue")},
    {path:"/4_login",
     name:'Login',
     title:'登录',
     component:()=>import("../views/4_login.vue")},
    {path:"/5_profile2",
     name:'Profile',
     title:'个人主页',
     component:()=>import("../views/5_profile2.vue"),
     //下方代码可以被替换为最下面被注释的代码
     //全局前置守卫:当用户访问 /5_profile2 页面（个人主页）时,会检查用户是否已登录,如果已登录则允许访问;如果未登录则强制跳转到登录页面 /4_login
     beforeEnter:(to,from,next)=>{       
        const user = userStore();
        if (user.isLoggedIn) {
            next();
        } else {
            next("/4_login");
        }
     },

    },
    {path:"/6_mall2",
     name:'Mall',
     title:'商场',
     component:()=>import("../views/6_mall2.vue")},
    {path:"/user/list",
      name:"UserList",
      title:"用户列表",
      component:()=> import("@/views/user/List.vue"),
    },
    {path:"/user/:id",
      name:"UserDetail",
      title:"用户详情",
      component:()=> import("@/views/user/Detail.vue"),
    },
    
];

const router=createRouter({
        history:createWebHistory(),
        routes
})

//全局前置守卫:当用户访问 /5_profile2 页面（个人主页）时,会检查用户是否已登录,如果已登录则允许访问;如果未登录则强制跳转到登录页面 /4_login
// router.beforeEach((to,from,next)=>{
//     const user = userStore();
//     if(to.path==='/5_profile2'){
//         if(user.isLoggedIn==true){ //isLoggedIn 登录状态
//             next();
//         }else{
//             next({path:"/4_login"});
//         }
//     }else{
//         next();
//     }
// })

export default router;